<?php

namespace app\index\controller;

use think\Cache;
use think\Controller;
use think\Db;
use think\Loader;
use think\Validate;

class Index extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    /**
     *
     */
    public function test()
    {
        $token = input("__token__");//获取提交过来的令牌
        $data = [
            '__token__' => $token,
        ];
        //验证路由
        $rule = [
            '__token__' => 'token',
        ];
        //错误信息
        $msg = [
            '__token__' => '请不要刷新该页面！',
        ];
        //引入验证类
        $validate = Validate::make($rule, $msg);
        if (!$validate->check($data)) {
            //如果验证不通过则错误页面并显示错误信息，3s后跳转指定页面
            echo '没通过';
            exit;
        } else {
            //通过后则正常进行
            echo 1;
            exit;
        }
    }

    public function test2()
    {
        $phpWord = \PhpOffice\PhpWord\IOFactory::load('./testword.docx');
        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
        $xmlWriter->save('./ceshi.htm');
    }


    /**
     *
     */
    public function test3()
    {
        Loader::import('wxsdk\wxPay', EXTEND_PATH);

        $obj = new \wxPay();

        $obj->jsApi(1, 2, 0, 0, 0);

    }

    public function skuApi()
    {
        $input = Request()->param();
        $id = $input['id'];

        if (empty($id)) apiReturn(2, '商品ID缺失');

        $info = Db::name('goods')->where(['id' => $id])->find();
        if ($info) {
            $info['img'] = getPicture($info['img']);

            $sku_data = getSku($id);

            $data = [
                'info' => $info,
                'sku_list' => $sku_data['sku_list'],
                'sku_value_list' => $sku_data['sku_value_list']
            ];

            apiReturn(1, '获取数据成功', $data);
        } else {
            apiReturn(2, '商品不存在或已删除');
        }
    }

    public function privacyAgreement()
    {
    	$content = Db::name('ysxy')->where(['id' => 1])->value('content');

    	$this->assign('content', $content);

    	return $this->view->fetch();
    }

}
